package cn.gson.springboot.model.service.presaleservice;

import cn.gson.springboot.model.dao.presaledao.*;
import cn.gson.springboot.model.mapper.presalemapper.PsBusinessDetailsMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.vo.presalevo.HistoryProducts;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 商机详情业务类
 */
@Service
@Transactional
public class PsBusinessDetailsService {
    @Autowired
    PsBusinessDetailsMapper businessDetailsMapper;//注入商机详情mapper
    @Autowired
    BusinessInfoDao businessInfoDao;//注入商机dao

    @Autowired
    FollowUpDao followUpDao;//注入跟进记录dao

    @Autowired
    QuickDao quickDao;//注入快捷信息dao

    @Autowired
    DemandDao demandDao;//注入详细需求dao

    @Autowired
    CompetitorDao competitorDao;//注入竞争对手dao

    @Autowired
    ProgrammeDao programmeDao;//注入方案dao

    @Autowired
    PsBusinessDatailsTwoService businessDatailsTwoService;//注入商机详情2业务service

    @Autowired
    CompetingProductsDao competingProductsDao;//注入竞品dao

    @Autowired
    TimelineDao timelineDao;//时间线dao


    //查询所有的跟进类型
    public List<FollowType> allFollowType(){
        return businessDetailsMapper.allFollowType();
    }

    //根据跟进类型查询出对应的快捷添加语言
    public List<Quick> quickAdd(int follId){
        return businessDetailsMapper.quickAdd(follId);
    }

    //新增商机的跟进记录
    public int saveFollow(FollowUp followUp){
        followUpDao.save(followUp);
        Integer count=businessDatailsTwoService.countFollowUp(followUp.getBusinessInfoByBusinesId().getBusinesId());
        if(count==2){
            //跟进记录达到两条，推进进度深度沟通
            SpeedInfo speedInfo=businessDatailsTwoService.selectOne("深度沟通");
            BusinessInfo businessInfo = businessInfoDao.findById(followUp.getBusinessInfoByBusinesId().getBusinesId()).get();
            businessInfo.setSpeedInfoBySpeedId(speedInfo);
            businessInfoDao.save(businessInfo);
            return 1;
        }
        BusinessInfo businessInfo1 = businessInfoDao.findById(followUp.getBusinessInfoByBusinesId().getBusinesId()).get();
        businessInfo1.setUpdateTime(new Timestamp(new Date().getTime()));
        businessInfoDao.save(businessInfo1);
        return 0;
    }

    //新增快捷模板
    public Integer savaQuick(Quick quick){
      Integer quick1=isNotQuick(quick.getQuickContext());
        System.err.println("zhi:"+quick1);
        //如果对象为null，说明快捷信息中不存在，则可以新增 就返回0
        if(quick1==0){
            System.err.println("不存在的可以新增！");
            quickDao.save(quick);
            return 0;
        }else{
            return 1;
        }
        //否则返回存在

    }
    /*删除快捷模板*/
    public void deleteQuick(Integer quickId){
        Quick quick=quickDao.findById(quickId).get();
        quickDao.delete(quick);
    }
    /*查询该商机的跟进记录*/
    public List<FollowUp> selectFollowUp(int businesId){
        return businessDetailsMapper.selectFollowUp(businesId);
    }

    /*查询快捷信息是否存在*/
    public Integer isNotQuick(String quickContext){
        return businessDetailsMapper.isNotQuick(quickContext);
    }

    /*查询对应跟进类型的快捷信息*/
    public List<Quick> allQuicks(){
        return businessDetailsMapper.allQuicks();
    }

   
    /**新增或修改竞争关系*/
    public void saveCompetitor(Competitor competitor){
        competitor.setCompetitorStatus("1");
        competitorDao.save(competitor);
        BusinessInfo businessInfo1 = businessInfoDao.findById(competitor.getBusinessInfoByBusinesId().getBusinesId()).get();
        businessInfo1.setUpdateTime(new Timestamp(new Date().getTime()));
        businessInfoDao.save(businessInfo1);
        Timeline timeline = new Timeline();//时间线
        timeline.setCustomerId(competitor.getCustomerByCustomId().getCustomId());
        timeline.setTimelineTitle("新增了竞争关系:");
        timeline.setTimelineContent(competitor.getCompetitorProduct().getComProductsName());
        timeline.setSort(3);//排序
        timeline.setTimelineCreatetime(new java.sql.Date(new Date().getTime()));//记录时间
        timelineDao.save(timeline);
    }
    //删除竞争对手
    public void deleteCompetitor(Integer competitorId){
        Competitor competitor=competitorDao.findById(competitorId).get();
        competitor.setCompetitorStatus("0");
        competitorDao.save(competitor);
    }

    /**查询商机的竞争对手*/
    public List<Competitor> competitorList(int businesId){
        return businessDetailsMapper.competitorList(businesId);
    }

    /**方案的新增或修改*/
    public int saveProgramme(Programme programme){
        if(programme.getProgrammeId()==0){
            programme.setProgrammeStauts("1");
            programme.setProgrammeTime(new Timestamp(new Date().getTime()));
        }else{
            Programme programme1 = programmeDao.searchProgramme(programme.getProgrammeId());
            programme.setProgrammeTime(programme1.getProgrammeTime());
            programme.setProgrammeStauts(programme1.getProgrammeStauts());
        }

        programmeDao.save(programme);
        Integer count = businessDatailsTwoService.countProgramme(programme.getBusinessInfoByBusinesId().getBusinesId());
        if (count==0){
            //有一个解决方案，推进进度方案设计
            SpeedInfo speedInfo = businessDatailsTwoService.selectOne("方案设计");
            BusinessInfo businessInfo=businessInfoDao.findById(programme.getBusinessInfoByBusinesId().getBusinesId()).get();
            businessInfo.setSpeedInfoBySpeedId(speedInfo);
            businessInfoDao.save(businessInfo);
            return  1;
        }
        BusinessInfo businessInfo1 = businessInfoDao.findById(programme.getBusinessInfoByBusinesId().getBusinesId()).get();
        businessInfo1.setUpdateTime(new Timestamp(new Date().getTime()));
        businessInfoDao.save(businessInfo1);
        return 0;
    }
    /**查询商机的解决方案*/
    public List<Programme> programmeList(int businesId){
        return businessDetailsMapper.programmeList(businesId);
    }
    /**查询商机下的客户联系人*/
    public List<BusinessInfo> select_culinkman(int businesId){
        return businessDetailsMapper.select_culinkman(businesId);
    }
    /*查询客户所有的联系人以及联系人的模糊查询*/
    public List<Culinkman> culinkmanList(Integer customerId,String cupName){
        return businessDetailsMapper.culinkmanList(customerId,cupName);
    }

    /*修改商机的绑定的客户联系人*/
    public void updateCulinkman(Integer businesId,Integer cupId){
        Culinkman culinkman1=new Culinkman();
        culinkman1.setCupId(cupId);
        //查询该商机
        BusinessInfo businessInfo=businessInfoDao.findById(businesId).get();
        businessInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        //修改联系人
        businessInfo.setCulinkmanByCupId(culinkman1);
        businessInfoDao.save(businessInfo);
    }

    public void saveDemand(Demand demand) {
        Timestamp time=new Timestamp(new Date().getTime());
        if(demand.getDemandId() ==0 ){//新增
            demand.setDemandStatus(1);
            demand.setDemandTime(time);
        }else{
            Demand demand1 = demandDao.searchDemand(demand.getDemandId());
            demand.setDemandTime(demand1.getDemandTime());
            demand.setDemandStatus(demand1.getDemandStatus());
        }
        demandDao.save(demand);
        //更新商机时间
        BusinessInfo businessInfo = businessInfoDao.findById(demand.getBusinessInfoByBusinesId().getBusinesId()).get();
        businessInfo.setUpdateTime(time);
        businessInfoDao.save(businessInfo);
    }

    /**
     * 商机下的详细需求
     * @param businesId
     * @return
     */
    public List<Demand> queryBusinessDemand(Integer businesId) {
        return businessDetailsMapper.queryBusinessDemand(businesId);
    }

    /**
     * save 竞品
     * @param competingProducts
     */
    public void saveComProducts(CompetingProducts competingProducts) {
        if(competingProducts.getComProductsId()==0){
            competingProducts.setComTime(new Timestamp(new Date().getTime()));
            competingProducts.setComDelete(1);
        }else{
            CompetingProducts competingProducts1 = competingProductsDao.queryComproducts(competingProducts.getComProductsId());
            competingProducts.setComDelete(competingProducts1.getComDelete());
            competingProducts.setComTime(competingProducts1.getComTime());
        }
        competingProductsDao.save(competingProducts);
    }
    /**
     * 查询竞品名称是否重复
     */
    public int isNotComProductsName(String comProductName){
        CompetingProducts competingProducts = competingProductsDao.isNotComproductsName(comProductName);
        if(competingProducts == null){
            return  0 ;//不存在返回0
        }else{
            return 1;//存在返回1
        }

    }

    /**
     * 查询竞品
     * @param comProductsName
     * @return
     */
    public List<CompetingProducts> allComProducts(String comProductsName){
        return businessDetailsMapper.allComProducts(comProductsName);
    }

    /**
     * 查询所有的竞争公司
     * @return
     */
    public List<Competitor> selectAllCompetition(){
        return  businessDetailsMapper.selectAllCompetition();
    }

    /**
     * 删除详细需求
     * @param demandId
     */
    public void deleteDemand(Integer demandId) {
        Demand demand = demandDao.findById(demandId).get();
        demand.setDemandStatus(0);
        demandDao.save(demand);
    }

    /**
     * 查看客户的历史产品
     * @param prName
     * @return
     */
    public List<HistoryProducts> customerHistoryProducts(Integer customId,String prName){
        return businessDetailsMapper.customerHistoryProducts(customId,prName);
    }
}
